<?php
namespace app\admin\controller;
use think\Db;
use think\Validate;
class Auth
{
	public function __construct(){
		Admin_auth();
	}

    public function index()
    {
        $aid = input('get.aid');
        $phone = input('get.phone');
        $where = [];
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        if(!empty($phone)){
            $where['phone'] = $phone;
        }
        $pages = pages('admini',$where,input('get.page'));
        $data = Db::table('admini')->whereIn('aid',session('aid'))->where($where)->limit($pages['limit'])->order('id desc')->select();
        return view('',['data'  => $data,'pages' => $pages['pages']]);
    }

    public function edit()
    {
        $id = input('get.id')?input('get.id'):0;
        $menu = array();
        $province = Db::table('province')->where('status',1)->whereIn('id',session('aid'))->select();
        $city = Db::table('city')->where('status',1)->whereIn('id',session('aid'))->select();
        $area = Db::table('area')->where('status',1)->whereIn('id',session('aid'))->select();
        $topmenu = Db::table('admin_menu')->where('gid',0)->select();
        if(!empty($topmenu)){
            foreach($topmenu as $sub){
                $sub['sub'] = Db::table('admin_menu')->where('gid',$sub['id'])->select();
                array_push($menu,$sub);
            }
        }
        if(request()->isPost()){
            $data = input('post.');
            if($data['type'] == 'all'){
                $validate = new Validate([
                    ['auth','require',lang('label')['auth'].lang('currency')['required']],
                ]);
                $data['auth'] = implode(',',$data['auth']);
            }else{
                $data['all_aid'][] = $data['aid'];
                $validate = new Validate([
                    ['aid','require',lang('currency')['city'].lang('currency')['required']],
                    ['auth','require',lang('label')['auth'].lang('currency')['required']],
                ]);
                if($data['type'] == 'province'){
                    $city_str = Db::table('city')->where(['father_id'=>$data['aid']])->column('id');
                    $data['all_aid'] = array_merge($data['all_aid'],$city_str);
                    if(!empty($city_str)){
                        foreach($city_str as $cid){
                            $area_str = Db::table('area')->where(['father_id'=>$cid])->column('id');
                            $data['all_aid'] = array_merge($data['all_aid'],$area_str);
                        }
                    }
                }elseif($data['type'] == 'city'){
                    $city_str = Db::table('area')->where(['father_id'=>$data['aid']])->column('id');
                    $data['all_aid'] = array_merge($data['all_aid'],$city_str);
                }
                $data['auth'] = implode(',',$data['auth']);
                $data['all_aid'] = implode(',',$data['all_aid']);
            }
            if (!$validate->check($data)) {
                return error($validate->getError());
            }
            if(Db::table('admini')->where(['phone' => $data['phone']])->where('id','<>',$id)->find()){
                return error(lang('tip')['re_phone']);
            }
            if($id){
                if(empty($data['password'])){
                    unset($data['password']);
                }else{
                    $data['password'] = md5($data['password']);
                }
                Db::table('admini')->where(['id' => $id])->update($data);
                $data['id'] = $id;
                $data['password'] = '';
            }else{
                $data['time'] = time();
                $data['password'] = md5($data['password']);
                $data['id'] = Db::table('admini')->insertGetId($data);
            }
        }
        $data = Db::table('admini')->where(['id'=>['>',1],'id' => $id])->find();
        return view('',['data'  => $data,'menu_all' => $menu,'province' => $province,'city'=>$city,'area'=>$area]);
    }
	
	public function del()
    {
		$id = input("get.id")?input("get.id"):0;
		$page = input("get.page");
		Db::table("admini")->delete($id);
		redirect(url('/admin/auth').'?page='.$page);
		exit;
    }
}
